<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */

    private $_id;

	public function authenticate()
	{
        //Get the user from sso Check phone
        $user = TblSsoUser::model()->findByAttributes(array('phone'=>$this->username));
        $userInfo = TblDoctors::model()->findByAttributes(array('phone'=>$this->username));
        $doctorInfo = TblDoctorInformation::model()->findByAttributes(array('pk_fk_doctor_id'=>$userInfo->pk_doctor_id));
        //$userRole = TblDoctorsHasPmsRoles::model()->findByAttributes(array('tbl_fk_doctor_id'=>$userInfo->pk_doctor_id));
        if($user === null)//No user found
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else if ($userInfo === null)// No user found
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else if ($userInfo->doctor_status != 1 && $userInfo->doctor_status != 2 && $userInfo->doctor_status != 3  && $userInfo->doctor_status !=6  && $userInfo->doctor_status !=7 && $userInfo->doctor_status != 8) // No available status found
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if ($user->password !== sha1(md5($this->password).'_TYS_'.md5($this->password)))// incorrect pass
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else if ($userInfo->doctor_status == 1 || $userInfo->doctor_status == 2 || $userInfo->doctor_status == 8 || $userInfo->doctor_status == 10)// correct role
        {

            $this->_id = $user->pk_user_id;//Storing user id
            $this->errorCode = self::ERROR_NONE;

            // Saving last_login datetime
//            $user->last_login_time = new CDbExpression('NOW()');// Saving date and time
//            $user->save(false); // Save the change to database

            //Action logging
//            $log = new AdminHistory(); //Create new empty record
//            $log->action = 'Login';//Saving the action type
//            $log->create_time = new CDbExpression('NOW()'); //Saving date and time
//            $log->user_ip = Yii::app()->request->UserHostAddress; //Saving ip_address
//            $log->fk_admin_user_id = $this->_id; //Saving user id
//            $log->save(false); //Saving the entire record with validation

            //Saving session information
            Yii::app()->session['phone'] = $user->phone;
            Yii::app()->session['true_name'] = $doctorInfo->true_name;
            Yii::app()->session['id'] = $userInfo->pk_doctor_id;
            Yii::app()->session['username'] = $doctorInfo->username;
            Yii::app()->session['status'] = 'logged';
            Yii::app()->session['logintime'] = time();

//            Yii::app()->session['role'] = $userRole->pms_fk_role_id;//Setting user role




//            Yii::app()->session['authorities'] = ;
        }else // others
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        return !$this->errorCode;
    }

    public function getId(){
        return $this->_id;
    }

}